/**
 * favorite action
 */

var favorite = new Function();
favorite.prototype.init = function(req, res){
	this.req = req;
	this.res = res;
	switch (req.method) {
		case 'GET': this.get(req, res); break;
		case 'POST': this.post(req, res); break;
		case 'DELETE': this.delete(req, res); break;
		default: this.other(req, res); break;
	}
};

favorite.prototype.get = function(req, res){
	var query = require('url').parse(req.url,true).query;
	var id = query.id;
	var getFavorites = new (Services('getFavorites'))(id, {
		success: function(data){
			res.send(JSON.stringify(data));
			res.end();
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};

favorite.prototype.post = function(req, res){
	var accountId = req.body.accountId;
	var placeId = req.body.placeId;
	var cfService = new (Services('addFavorite'))(accountId, placeId, {
		success: function(data){
			res.send(JSON.stringify({ success: true, data: data }));
			res.end();
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};

favorite.prototype.delete = function(req, res){
	var id = req.body.id;
	var dfService = new (Services('deleteFavorite'))(id, {
		success: function(data){
			res.send(JSON.stringify({ success: true, data: data }));
			res.end();
		},
		error: function(code, message){
			throw new Exception(code, message, function(){
				res.send(this.toString());
				res.end();
			});
		}
	});
};

favorite.prototype.other = function(req, res){
	res.writeHead(404, {
		'Content-Type': 'text/plain',
		'Trailer': 'Content-MD5'
	});
	res.end();
};

module.exports = favorite;